local skel = fk.CreateSkill {
  name = "emo__feihuamark&",
}

Fk:loadTranslationTable{
  ["emo__feihuamark&"] = "绯花",
  [":emo__feihuamark&"] = "出牌阶段，你可以失去一点体力视为使用一张【杀】。每当你或可可莉克共计造成3点伤害后，可可莉克可以令一名“绯花”角色回复一点体力。",

  ["#emo__feihuamark"] = "绯花:你可以失去一点体力视为使用一张【杀】",
  ["#emo__feihuamark-choose"] = "绯花:可以令一名“绯花”角色回复一点体力",
}

skel:addEffect("viewas", {
  anim_type = "offensive",
  mute_card = false,
  pattern = "slash",
  prompt = "#emo__feihuamark",
  card_filter = Util.FalseFunc,
  view_as = function (self, player, cards)
    local c = Fk:cloneCard("slash")
    c.skillName = self.name
    return c
  end,
  before_use = function (self, player, use)
    player.room:loseHp(player, 1, skel.name)
  end,
  enabled_at_play = function (self, player)
    return player.hp > 0
  end,
  enabled_at_response = function (self, player, response)
    return player.hp > 0 and not response and player.phase == Player.Play
  end,
})

skel:addEffect(fk.Damage, {
  anim_type = "support",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return not player.dead and ((data.extra_data or {}).emo__feihua_invokers or {})[player.id]
  end,
  on_cost = Util.TrueFunc,
  trigger_times = function (self, event, target, player, data)
    return ((data.extra_data or {}).emo__feihua_invokers or {})[player.id] or 0
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = {}
    for _, pid in ipairs(player:getTableMark("emo__feihua_tar")) do
      local to = room:getPlayerById(pid)
      if to:isAlive() then
        table.insert(tos, to)
      end
    end
    if #tos == 0 then return end
    tos = room:askToChoosePlayers(player, {
      targets = tos, max_num = 1, min_num = 1, skill_name = skel.name, cancelable = true,
      prompt = "#emo__feihuamark-choose",
    })
    if #tos > 0 then
      player:broadcastSkillInvoke("emo__yumeiren")
      room:recover { num = 1, skillName = skel.name, who = tos[1], recoverBy = player }
    end
  end,
})

--- 伤害记录器
local skel2 = fk.CreateSkill {
  name = "#emo__feihua_record",
}

skel:addEffect(fk.Damage, {
  can_refresh = function (self, event, target, player, data)
    return target == player
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local oldDamage = player:getMark("emo__feihua_damage") -- 总计造成伤害数
    local newDamage = oldDamage + data.damage
    room:setPlayerMark(player, "emo__feihua_damage", newDamage)

    -- 以下为处理3点伤害触发一次回血的效果
    local invokers = {}
    -- 我自己造成伤害
    if table.find(player:getTableMark("emo__feihua_tar"), function (pid)
      return room:getPlayerById(pid):isAlive()
    end) then
      local num = (newDamage // 3) - (oldDamage // 3)
      if num > 0 then
        invokers[player.id] = num
      end
    end
    -- 我的绯花目标造成伤害
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if table.contains(p:getTableMark("emo__feihua_tar"), player.id) then
        local old = p:getMark("emo__feihua_damage")
        local new = old + data.damage
        local n = (new // 3) - (old // 3)
        if n > 0 then
          invokers[p.id] = n
        end
      end
    end
    if next(invokers) ~= nil then
      data.extra_data = data.extra_data or {}
      data.extra_data.emo__feihua_invokers = invokers
    end
  end,
})



return {skel, skel2}
